// pages/detail/detail.js
let network = require("../../utils/network.js");
var WxParse = require('../../wxParse/wxParse.js');

let app = getApp();
let url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: [
      { "image": 'detail_img.png' },
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    currentSwiper: 0,
    addToCart:{},
    num:1,
    goods_id:0,
    goodslist:{},
    collect:false,
    cartNum:0
  },
  // banner对应的点
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    that.setData({
      goods_id:options.id,
      discount: wx.getStorageSync('discount')
    })
    that.goodsDetail(options.id)
  },

  goodsDetail(cal){
    var that = this
    network.request({
      url: url+'/AppletsApi/Goods/goodsDetail.html',
      data: {
        goods_id: cal,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        that.setData({
          goodslist: data.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.setStorageSync('goods_num', that.data.num)
    wx.setStorageSync('goods_id', that.data.goods_id),
    that.sum()
  },

  sum(){
    var that = this
    network.request({
      url: url + '/AppletsApi/Goods/shopCartList.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        var sum = 0
        for (var i = 0; i < data.data.data.length; i++) {
          sum += parseInt(data.data.data[i].goods_num)
        }
        that.setData({
          cartNum: sum
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // num--
  minus:function(){
    var that = this
    var num = that.data.num;
    if (num==0){
      wx.showToast({
        title: '受不了了,宝贝不能再减少了哦',
        icon: 'none',
        duration: 1000
      })
      this.setData({
        num: num
      })
      wx.setStorageSync('goods_num', that.data.num)
    }else{
      this.setData({
        num: num - 1
      })
      wx.setStorageSync('goods_num', that.data.num)
    }
  },
  // num++
  add:function(){
    var that = this
    var num = that.data.num;
    this.setData({
      num: num + 1
    })
    wx.setStorageSync('goods_num', that.data.num)
  },
  // 
  gocart:function(e){
    var that = this
    let path = e.currentTarget.dataset.path
    network.request({
      url: url +'/AppletsApi/Goods/addToCart.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        goods_id: wx.getStorageSync('goods_id'),
        goods_num: wx.getStorageSync('goods_num'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        if (wx.getStorageSync('goods_num') == 0){
          wx.showToast({
            title: '商品添加数量不能为0',
            icon: 'none',
            duration: 2000
          })
        }else{
          that.sum()
          wx.showToast({
            title: '已加入购物车',
            icon: 'success',
            duration: 1000
          })
        }
      }
    })
  },

  // 更新用户
  // getset(){

  // },

  // 授权
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      wx.setStorageSync('key', e.detail.userInfo)
      var path = e.currentTarget.dataset.path
      network.request({
        url: url +'/AppletsApi/Login/replenishUserInfo.html',
        data: {
          applet_openid: wx.getStorageSync('applet_openid'),
          nickName: wx.getStorageSync('key').nickName,
          avatarUrl: wx.getStorageSync('key').avatarUrl,
          gender: wx.getStorageSync('key').gender == 0 ? '未知' : wx.getStorageSync('key').gender == 1 ? '男性' : wx.getStorageSync('key').gender == 2 ? '女性' : '',
        },
        method: 'post',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (data) {
          wx.navigateTo({
            url: path,
          })
        }
      })
      

    } else {
      //用户按了拒绝按钮
    }
  },
  to_gwc:function(e){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  shoucang:function(e){
    var that = this
      network.request({
        url: url +'/AppletsApi/Collection/collectionAdd.html',
        data: {
          applet_openid: wx.getStorageSync('applet_openid'),
          goods_id:that.data.goods_id,
        },
        method: 'post',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (data) {
          that.goodsDetail(that.data.goods_id)
          // 收藏成功刷新页面
          if (data.data.code==449){
            that.setData({
              collect: true
            })
            wx.showToast({
              title: data.data.msg,
              icon: 'none',
              duration: 2000
            })
          }else{
            that.setData({
              collect: true
            })
            wx.showToast({
              title: '收藏成功',
              icon: 'success',
              duration: 2000
            })
          }

        }
      })


  }

})